﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using CaiBaby.Common;
using CaiBaby.Entity;

namespace CaiBaby.DAL
{
    /// <summary>
    /// 用户数据访问层
    /// </summary>
    public class User
    {
        /// <summary>
        /// 创建一个新用户
        /// </summary>
        /// <param name="userInfo">用户实体</param>
        /// <returns>返回用户ID</returns>
        public int Add(UserInfo userInfo)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO us_User(");
            strSql.Append("PassWord,NickName,Treasure,Email,LoginSeries,CreateTime)");
            strSql.Append(" VALUES (");
            strSql.Append("@PassWord,@NickName,@Treasure,@Email,@LoginSeries,@CreateTime)");
            strSql.Append(";SELECT @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@PassWord", SqlDbType.VarChar,32),
					new SqlParameter("@NickName", SqlDbType.VarChar,32),
					new SqlParameter("@Email", SqlDbType.VarChar,32),
					new SqlParameter("@CreateTime", SqlDbType.DateTime)};
            parameters[0].Value = userInfo.PassWord;
            parameters[1].Value = userInfo.NickName;
            parameters[3].Value = userInfo.Email;
            parameters[4].Value = userInfo.CreateTime;

            object obj = SQLHelper.GetSingle(ConfigHelper.CaiBabyDBConnStr,CommandType.Text,strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }


    }
}
